package com.beck.code.generator.generatorProcessor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.beck.code.generator.configProperty.ContextConfig;
import com.beck.code.generator.configProperty.TableConfig;
import com.beck.code.generator.vo.MapperXmlVO;


/**
 * @author CuiJianbo
 * @since 2025/7/1
 */

public class MapperXmlGenerator extends CommonGenerator {

    public MapperXmlGenerator(TableConfig tableConfig, IntrospectedTable introspectedTable, ContextConfig contextConfig) {
        super(tableConfig, introspectedTable, contextConfig, contextConfig.getMapperXmlGeneratorConfig());
    }

    public static MapperXmlGenerator getInstance(TableConfig tableConfig, IntrospectedTable introspectedTable, ContextConfig contextConfig) {
        return new MapperXmlGenerator(tableConfig, introspectedTable, contextConfig);
    }

    @Override
    void generateCodeFile() {
        String daoPackage = contextConfig.getDaoGeneratorConfig().getTargetPackage();
        String voPackage = contextConfig.getVoGeneratorConfig().getTargetPackage();
        MapperXmlVO mapperXmlVO = new MapperXmlVO();
        BeanUtil.copyProperties(commonGeneratorVO, mapperXmlVO);
        mapperXmlVO.setAssociations(associations);
        mapperXmlVO.setCollections(collections);
        mapperXmlVO.setFields(fields);
        mapperXmlVO.setNamespace(StrUtil.concat(true, daoPackage, ".", entityName, "Dao"));
        mapperXmlVO.setFullVoJavaName(StrUtil.concat(true, voPackage, ".", entityName, "VO"));
        mapperXmlVO.setFullVoJavaName(StrUtil.concat(true, voPackage, ".", entityName, "VO"));
        String data = applyTemplateResources(contextConfig.getMapperXmlGeneratorConfig().getTemplate(), mapperXmlVO);
        //输出真实文件
        boolean flag = generateCodeFile(data);

        if (flag) {
            System.out.println(fileName());
        }
    }

    @Override
    String fileName() {
        String fileName = StrUtil.concat(true, StrUtil.upperFirst(entityName), "Mapper.xml");
        return contextConfig.getMapperXmlGeneratorConfig().getTargetDirectory().replace(".", "/") + "/" + fileName;
    }
}
